package com.hui.platform.falseworkapi.common.core.vo;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.List;

/**
 * 通用分页返回的实体类
 *
 * @param <T>
 * @author Peng
 */
@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@ApiModel(value = "分页结果对象模型")
@Accessors(chain = true)
public class PageData<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @JSONField(ordinal = 1)
    @ApiModelProperty(value = "页码", position = 1)
    private Long pageNum;

    @JSONField(ordinal = 2)
    @ApiModelProperty(value = "每页大小", position = 2)
    private Long pageSize;

    @JSONField(ordinal = 3)
    @ApiModelProperty(value = "总页数", position = 3)
    private Long totalPage;

    @JSONField(ordinal = 4)
    @ApiModelProperty(value = "总记录数", position = 4)
    private Long total;

    @JSONField(ordinal = 5)
    @ApiModelProperty(value = "每页的记录数据的集合", position = 5)
    private List<T> rows;

    /**
     * 构造方法的静态封装
     *
     * @param pageNum   页码
     * @param pageSize  每页大小
     * @param totalPage 总页数
     * @param total     总记录数
     * @param rows      每页的记录数据的集合
     * @param <T>       数据泛型
     * @return 分页数据封装
     */
    public static <T> PageData<T> build(Long pageNum, Long pageSize, Long totalPage, Long total, List<T> rows) {
        return new PageData<T>()
                .setPageNum(pageNum)
                .setPageSize(pageSize)
                .setTotalPage(totalPage)
                .setTotal(total)
                .setRows(rows);
    }

    /**
     * 根据Mybatis-Plus分页构造分页参数
     *
     * @param page 分页集
     * @param rows 转换的BO的rows
     * @param <T>  泛型
     * @return 转换的BO数据
     */
    public static <T> PageData<T> build(IPage<?> page, List<T> rows) {
        return new PageData<T>()
                .setPageNum(page.getCurrent())
                .setPageSize(page.getSize())
                .setTotalPage(page.getPages())
                .setTotal(page.getTotal())
                .setRows(rows);
    }
}
